package cn.explink.dop.controller.app;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.enums.DeliveryManStatusEnum;
import cn.explink.dop.service.DeliveryManService;

/**
 *
 * @author songkaojun 2015年3月19日
 */
@Controller
@RequestMapping("/deliveryManController4App")
public class DeliveryManController4App {

	@Autowired
	private DeliveryManService deliveryManService;

	@RequestMapping(params = "setCoordinate")
	@ResponseBody
	public boolean setCoordinate(String userId, String coordinate, HttpServletRequest request) {
		if ((null == userId) || (null == coordinate)) {
			return false;
		}
		try {
			DeliveryManVO deliveryManVO = this.deliveryManService.queryByUserId(userId.trim());
			if (deliveryManVO != null) {
				deliveryManVO.setCoordinate(coordinate);
				this.deliveryManService.saveOrUpdate(deliveryManVO);
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@RequestMapping(params = "setStatus")
	@ResponseBody
	public Integer setStatus(String userId, Integer status, HttpServletRequest request) {
		if ((null == userId) || (null == status)) {
			return DeliveryManStatusEnum.CANNOT_ACCEPT.getValue();
		}
		try {
			DeliveryManVO deliveryManVO = this.deliveryManService.queryByUserId(userId);
			deliveryManVO.setStatus(status);
			this.deliveryManService.saveOrUpdate(deliveryManVO);

			return status;
		} catch (Exception e) {
			e.printStackTrace();

			return DeliveryManStatusEnum.CANNOT_ACCEPT.getValue();
		}
	}

	/**
	 *
	 * @Title: getHeadImg
	 * @Description: 获取人员头像，返回JSON，如果userId为空，返回空字符串,如果已经上传头像，返回头像地址，否则返回默认设置图片
	 * @author 孟凡岭
	 * @param @param userId
	 * @param @param request
	 * @param @return
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping(params = "getHeadImg")
	@ResponseBody
	public Map<String, Object> getHeadImg(String userId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("imgPath", "");
		if (StringUtils.isBlank(userId)) {
			return map;
		}
		DeliveryManVO deliveryManVO = this.deliveryManService.queryByUserId(userId.trim());
		map.put("imgPath", this.deliveryManService.getHeadImg(deliveryManVO.getId()));
		return map;
	}
}
